home *** CD-ROM | disk | FTP | other *** search
- Path: castle.nando.net!news
- From: actuary@nando.net (Bill McCarthy)
- Newsgroups: comp.lang.c
- Subject: Re: Newbie Question - Probably Dumb as well
- Date: 20 Jan 1996 03:54:14 GMT
- Organization: News & Observer Public Access
- Message-ID: <4dpp16$n4s@castle.nando.net>
- References: <194990319wnr@csmltd.demon.co.uk>
- Reply-To: actuary@nando.net (Bill McCarthy)
- NNTP-Posting-Host: grail713.nando.net
- X-Newsreader: IBM NewsReader/2 v1.2
-
- In <194990319wnr@csmltd.demon.co.uk>, Martyn Read <Martyn@csmltd.demon.co.uk> writes:
- >Hello,
- >Apologies if this is a dumb question but:
- >I have a comma delimited file like
- >
- >999,"AAAAAAAAAA AAAAAAAAA",999,999,999,"AA"
- >Int, String ,Int,Int,Int,String
- >
- >where all strings in the file have double quotes.
- >
- >Can I read the file into a struct using fscanf to ignore the double
- >quotes as well as the commas?
-
- If your string will always contain numbers and non-empty strings,
- then simply using fscanf(), or perhaps fgets() followed by sscanf(),
- is adequate. For example:
-
- #include <stdio.h>
-
- int main( void )
- {
- char a[] = "999,\"AAAAAAAAAA AAAAAAAAA\",999,999,999,\"AA\"",
- s1[100], s2[100];
- int i1, i2, i3, i4;
-
- sscanf( a, "%d,\"%[^\"]\",%d,%d,%d,\"%[^\"]\"",
- &i1, s1, &i2, &i3, &i4, s2 );
- printf( "%d %s %d %d %d %s\n", i1, s1, i2, i3, i4, s2 );
-
- return 0;
- }
-
- To write a robust routing, you should write your own parsing
- function - checking all return values of functions and reporting
- the location and nature of the error on failure.
-
- Bill McCarthy
- actuary@nando.net
- Wendell, NC USA
-